home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / fontutil.6 / fontutil / fontutils-0.6 / lib / bb-outline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-04  |  4.0 KB  |  113 lines

  1. /* bb-outline.c: find the bounding boxes enclosing outlines.
  2.  
  3. Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include "bb-outline.h"
  22. #include "edge.h"
  23.  
  24.  
  25. static bounding_box_type find_one_bb
  26.   (bitmap_type, edge_type, unsigned, unsigned, bitmap_type *);
  27.  
  28. /* A character is made up of a list of one or more outlines.  Here, we
  29.    go through a character's bitmap marking edges on pixels, top to
  30.    bottom, left to right, looking for the next pixel with an unmarked
  31.    edge also on the character's outline.  Each one of these we find is
  32.    the starting place for one outline.  We get the bounding boxes of the
  33.    outlines, and put them in a list to return.  We don't look at pixels
  34.    that are at or after the column LEFT_MARK or before RIGHT_MARK.  If
  35.    FIND_INNER is false, we return only outside outlines; otherwise, we
  36.    return both inside and outside.  */
  37.  
  38. bb_list_type
  39. find_outline_bbs (bitmap_type b, boolean find_inner,
  40.                   int left_mark, int right_mark)
  41. {
  42.   unsigned row, col;
  43.   bb_list_type bb_list = bb_list_init ();
  44.   bitmap_type marked = new_bitmap (BITMAP_DIMENSIONS (b));
  45.  
  46.   /* By looking through the bitmap in column-major order, we preserve
  47.      the original ordering of the characters in the image.  Otherwise,
  48.      `b' would be found before `a' (for example), because of the
  49.      ascender.  */
  50.   for (col = 0; col < BITMAP_WIDTH (b); col++)
  51.     if (col < left_mark || col >= right_mark)
  52.       for (row = 0; row < BITMAP_HEIGHT (b); row++)
  53.         {
  54.           edge_type edge;
  55.  
  56.           if (BITMAP_PIXEL (b, row, col) == WHITE
  57.               || BITMAP_INTERIOR_PIXEL (b, row, col))
  58.             continue;
  59.  
  60.           edge = next_unmarked_outline_edge (row, col, START_EDGE, b, marked);
  61.           
  62.           if (edge != no_edge)
  63.         {
  64.           /* Want to mark edges in inner boxes, too.  */
  65.           bounding_box_type bb = find_one_bb (b, edge, row, col, &marked);
  66.  
  67.           /* If edge != START_EDGE, it's on an inner bounding box.  */
  68.           if (find_inner || edge == START_EDGE)
  69.                 {
  70.                   /* The numbers in BB as it comes in refer to pixels;
  71.                      we have to change the maximum row so that it refers
  72.                      to the edge beyond the last pixel.  */
  73.                   MAX_COL (bb)++;
  74.  
  75.                   bb_list_append (&bb_list, bb);
  76.                 }
  77.         }
  78.         }
  79.  
  80.   free_bitmap (&marked);
  81.   return bb_list;
  82. }
  83.  
  84. /* Here we find one of a character's outlines.  We're passed the
  85.    position (ORIGINAL_ROW and ORIGINAL_COL) of a starting pixel and one
  86.    of its unmarked edges, ORIGINAL_EDGE.  We traverse the adjacent edges
  87.    of the outline pixels but keep the bounding box in pixel coordinates.
  88.    We keep track of the marked edges in MARKED, which should be
  89.    initialized to zeros before we get it.  */
  90.  
  91. static bounding_box_type
  92. find_one_bb (bitmap_type b, edge_type original_edge,
  93.              unsigned original_row, unsigned original_col,
  94.              bitmap_type *marked)
  95. {
  96.   bounding_box_type bb = { INT_MAX, INT_MIN, INT_MAX, INT_MIN };
  97.   unsigned row = original_row, col = original_col;
  98.   edge_type edge = original_edge;
  99.  
  100.   do
  101.     {
  102.       coordinate_type p = { col, row };
  103.       update_bounding_box (&bb, p);
  104.  
  105.       mark_edge (edge, row, col, marked);
  106.       next_outline_edge (b, &edge, &row, &col);
  107.     }
  108.   while (!(row == original_row && col == original_col
  109.            && edge == original_edge));
  110.  
  111.   return bb;
  112. }
  113.